#include "library.h"

//-------------------------------------------------------------------------------------------
Library::Library(QString path)
{
	if (!this->loadLibrary(path)){
		this->pieces_collection.clear();
		this->library_path = "";
		this->name = "noname";
	}
}

//-------------------------------------------------------------------------------------------
Library::Library()
{
	this->pieces_collection.clear();
	this->library_path = "";
	this->name = "noname";
}

//-------------------------------------------------------------------------------------------
Library::~Library()
{
}

//-------------------------------------------------------------------------------------------
QList<Piece> Library::getPiecesCollection()
{
	return this->pieces_collection;
}

//-------------------------------------------------------------------------------------------
Piece Library::getPieceAt(int index)
{
	return this->pieces_collection.at(index);
}

//-------------------------------------------------------------------------------------------
void Library::addPiece(Piece piece)
{
	this->pieces_collection += piece;
}

//-------------------------------------------------------------------------------------------
void Library::insertPiece(int index, Piece piece)
{
	this->pieces_collection.insert(index, piece);
}

//-------------------------------------------------------------------------------------------
void Library::removePiece(Piece piece)
{
	bool find = false;
	for (int i = 0; i < this->pieces_collection.size() && !find; i++){
		if (piece == this->pieces_collection.at(i)){
			find = true;
			this->pieces_collection.removeAt(i);
		}
	}
}

//-------------------------------------------------------------------------------------------
void Library::removePieceAt(int index)
{
	this->pieces_collection.removeAt(index);
}

//-------------------------------------------------------------------------------------------
void Library::removePieces()
{
	this->pieces_collection.clear();
}

//-------------------------------------------------------------------------------------------
void Library::replacePiece(int index, Piece newPiece)
{
	this->pieces_collection.replace(index, newPiece);
}

//-------------------------------------------------------------------------------------------
void Library::saveLibrary()
{
	QFile file(this->library_path);
	if (!file.open(QIODevice::WriteOnly))
		return;
		
	QTextStream out(&file);
	int n_pieces = this->pieces_collection.size();
	int n_vertexes;
		
	out << this->name << "\n";
	out << n_pieces << "\n";
		
	for (int i=0; i < n_pieces; i++){
		Piece piece = this->pieces_collection.at(i);
		n_vertexes = piece.count();
		out << piece.getColor().name() << "\n";
		out << n_vertexes << "\n";
		QList<QPointF> vertexes = piece.getVertexes();
		for (int j=0; j < n_vertexes; j++){
			QPointF point = vertexes.at(j);
			out << point.x() << "," << point.y() << "\n";
		}
	}	
	file.close();
}

//-------------------------------------------------------------------------------------------
void Library::saveLibraryAs(QString path)
{
	this->library_path = path;
	this->saveLibrary();
}

//-------------------------------------------------------------------------------------------
void Library::setPath(QString path)
{
	this->library_path = path;
}

//-------------------------------------------------------------------------------------------
void Library::setName(QString name)
{
	this->name = name;
}

//-------------------------------------------------------------------------------------------
bool Library::loadLibrary(QString path)
{
	QFile file(path);
	if (!file.open(QIODevice::ReadWrite))
		return false;
		
	this->library_path = path;
		
	QTextStream in(&file);
	int n_pieces = 0;
		
	this->name = in.readLine();
	n_pieces = in.readLine().toInt();
		
	for (int i=0; i < n_pieces; i++){
		QString colorName = in.readLine();
		Piece piece(colorName);
		int n_vertexes = 0;
		n_vertexes = in.readLine().toInt();
		for (int j=0; j < n_vertexes; j++){
			float x,y;
			QString line = in.readLine();
			x = line.section(",",0,0).toFloat();
			y = line.section(",",1,1).toFloat();
			QPointF point(x,y);
			piece.addVertex(point);
		}
		this->pieces_collection += piece;
	}	
	file.close();	
	return true;
}

//-------------------------------------------------------------------------------------------
int Library::count()
{
	return this->pieces_collection.size();
}

//-------------------------------------------------------------------------------------------
QString Library::getPath()
{
	return this->library_path;
}

//-------------------------------------------------------------------------------------------
QString Library::getName()
{
	return this->name;
}

//-------------------------------------------------------------------------------------------
//------------------------------ FUNCIONES GRALES -------------------------------------------
//-------------------------------------------------------------------------------------------
void Library::LoadPiecesList(QString library_path, QListWidget *list)
{
	Library *library = new Library(library_path);
	int count = library->count();
	list->clear();
	
	for (int i=0; i < count; i++){
		QVariant nro(i);
		Piece piece = library->getPieceAt(i);
		piece.translateMe(QPointF(0,0));
		QPixmap preview((int)piece.width()+1, (int)piece.height()+1);
		preview.fill(qRgb(255,255,255));
		QPainter *painter = new QPainter(&preview);
		piece.draw(painter,POLYGON);
		QListWidgetItem *itemlist = new QListWidgetItem(list);
		QString tooltiptext("Medidas: " + QVariant(piece.width()).toString() + "x" + QVariant(piece.height()).toString() + "\nArea: " + QVariant(piece.area()).toString());
		itemlist->setToolTip(tooltiptext);
		//itemlist->setText(nro.toString());
		itemlist->setIcon(QIcon(preview));
		itemlist->setData(Qt::UserRole, QVariant(preview));
		itemlist->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
	}
}

//--------------------------------------------------------------------------------------------
void Library::LoadLibraryList(QString libraries_dir, QListWidget *list)
{
	QDir dir = QDir(libraries_dir);
	QStringList libs = dir.entryList(QStringList("*.*"), QDir::Files);
	list->clear();
	list->addItems(libs);
}

//--------------------------------------------------------------------------------------------
void Library::LoadLibraryList(QString libraries_dir, QComboBox *list)
{
	QDir dir = QDir(libraries_dir);
	QStringList libs = dir.entryList(QStringList("*.*"), QDir::Files);
	list->clear();
	list->addItems(libs);
}
